<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"sekla.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="图的遍历图遍历又称图的遍历，属于数据结构中的内容。指的是从图中的任一顶点出发，对图中的所有顶点访问一次且只访问一次。图的遍历操作和树的遍历操作功能相似。图的遍历是图的一种基本操作，图的许多其它操作都是建立在遍历操作的基础之上。 图遍历可分为四种问题： 图的遍历可分为四类： （1）“一笔画问题”或“欧拉路径”； （2） “哈密尔顿问题”； （3）“中国邮递员问题”； （4）“旅行推销员问题”。 对于">
<meta property="og:type" content="article">
<meta property="og:title" content="图的遍历">
<meta property="og:url" content="http://sekla.cn/2021/04/14/grahp/index.html">
<meta property="og:site_name" content="Sekla&#39;s Blog">
<meta property="og:description" content="图的遍历图遍历又称图的遍历，属于数据结构中的内容。指的是从图中的任一顶点出发，对图中的所有顶点访问一次且只访问一次。图的遍历操作和树的遍历操作功能相似。图的遍历是图的一种基本操作，图的许多其它操作都是建立在遍历操作的基础之上。 图遍历可分为四种问题： 图的遍历可分为四类： （1）“一笔画问题”或“欧拉路径”； （2） “哈密尔顿问题”； （3）“中国邮递员问题”； （4）“旅行推销员问题”。 对于">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/04/14/dZitSrnlP7Y5sXk.png">
<meta property="og:image" content="https://i.loli.net/2021/04/14/R34iqleYaAKbyUg.png">
<meta property="og:image" content="https://i.loli.net/2021/04/14/2IEPsTCQzXqSfB8.png">
<meta property="og:image" content="https://i.loli.net/2021/04/14/puSMDRKN2Ohj6b9.png">
<meta property="og:image" content="https://i.loli.net/2021/04/14/9cpzlF5x7IHE8VD.png">
<meta property="article:published_time" content="2021-04-14T05:19:04.000Z">
<meta property="article:modified_time" content="2021-10-18T14:44:54.388Z">
<meta property="article:author" content="Sekla">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/04/14/dZitSrnlP7Y5sXk.png">

<link rel="canonical" href="http://sekla.cn/2021/04/14/grahp/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>图的遍历 | Sekla's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script type="text/javascript" src="/js/my_js/clicklove.js"></script>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Sekla's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Keep Learning Keep Doing</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">100</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://sekla.cn/2021/04/14/grahp/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Sekla">
      <meta itemprop="description" content="Sekla">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sekla's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          图的遍历
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-14 13:19:04" itemprop="dateCreated datePublished" datetime="2021-04-14T13:19:04+08:00">2021-04-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-18 22:44:54" itemprop="dateModified" datetime="2021-10-18T22:44:54+08:00">2021-10-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="图的遍历"><a href="#图的遍历" class="headerlink" title="图的遍历"></a>图的遍历</h1><p>图遍历又称图的遍历，属于数据结构中的内容。指的是从图中的任一顶点出发，对图中的所有顶点访问一次且只访问一次。图的遍历操作和树的遍历操作功能相似。图的遍历是图的一种基本操作，图的许多其它操作都是建立在遍历操作的基础之上。</p>
<p>图遍历可分为四种问题：</p>
<p>图的遍历可分为四类：</p>
<p>（1）“一笔画问题”或“欧拉路径”；</p>
<p>（2） “哈密尔顿问题”；</p>
<p>（3）“中国邮递员问题”；</p>
<p>（4）“旅行推销员问题”。</p>
<p>对于第一和第三类问题已经得到了完满的解决，而第二和第四类问题则只得到了部分解决。</p>
<a id="more"></a>

<h2 id="欧拉路径"><a href="#欧拉路径" class="headerlink" title="欧拉路径"></a>欧拉路径</h2><h3 id="问题概述"><a href="#问题概述" class="headerlink" title="问题概述"></a>问题概述</h3><p>首先是一个问题：即小学奥数中的一笔画问题，在一个图中能否用一笔画从一个顶点出发来经过所有的边，最后回到这个顶点。</p>
<p>这个问题是数学家欧拉在研究著名的德国哥尼斯堡七桥问题时发现的，流经哥尼斯堡的普雷格尔河中有两个岛，两个岛与两岸共4处陆地通过7座杨 彼此相联。7桥问题就是如何能从任一处陆地出发，经过且经过每个桥一次后回到原出发点。</p>
<p><img src="https://i.loli.net/2021/04/14/dZitSrnlP7Y5sXk.png" alt="EULY_1.png"></p>
<p>这个问题可抽象为一个如图b所示的数学意义上的图，其中4个结点分别表示与4块陆土Il 对应，如结点C对应河岸C，结点A对应岛A等，而结点之间的边表示7座桥。</p>
<p>欧拉通过研究这个问题，提出了几个重要的概念：</p>
<ul>
<li><strong>欧拉回路</strong>：<strong>从图上一个点u出发不重复地经过每一条边后，再次回到点u的一条路径。</strong></li>
<li><strong>欧拉路径</strong>：<strong>从图上一个点u出发不重复地经过每一条边的一条路径（不必回到点u）。</strong></li>
<li><strong>欧拉图即存在欧拉回路的图</strong></li>
<li><strong>半欧拉图存在欧拉路径的图。</strong></li>
</ul>
<h3 id="求解算法思想"><a href="#求解算法思想" class="headerlink" title="求解算法思想"></a>求解算法思想</h3><p>首先需要了解相关的定理：</p>
<p><strong>（1）无向图欧拉回路的判定：图G为连通图，所有顶点的度为偶数。</strong></p>
<p><strong>（2）无向图欧拉路径的判定：图G为连通图，除有2个顶点度为奇数外，其他顶点度都为偶数。</strong></p>
<p><strong>（3）有向图欧拉回路的判定：图G的基图联通，所有顶点的入度等于出度。</strong></p>
<p><strong>（4）有向图欧拉路径的判定：图G的基图联通，存在起点u的入度比出度小1，v终点入度比出度大一，其余所有顶点的入度等于出度。</strong></p>
<p>(忽略有向图所有边的方向，得到的无向图称为该有向图的基图)</p>
<p>求解是否含有欧拉路径的算法一般有两种：Fluery算法和Hierholzer算法。这里介绍Fluery算法。</p>
<p><strong>Fluery算法</strong></p>
<p>设G为一无向欧拉图，求G中一条欧拉回路的算法为：</p>
<ol>
<li><p>任取G中一顶点V0，令P0=V0；</p>
</li>
<li><p>假设沿路径Pi=v0e1v1e2v2….eivi走到顶点vi，按下面方法从E(G)-{e1,e2,e3…ei}中选择ei+1:</p>
<p>a.ei+1与vi相关联；</p>
<p>b.除非无别的边可供选择，否则ei+1不应该是Gi=G-{e1,e2,e3…ei}中的桥。</p>
</li>
<li><p>当2不能继续进行时算法停止。</p>
<p>可以证明当算法停止时，得到的简单回路Pm为G中的一条欧拉回路。</p>
</li>
</ol>
<p>(桥：一个边从无向图删除后图不再连通，这个边即为桥，也称割边)</p>
<h3 id="举例说明"><a href="#举例说明" class="headerlink" title="举例说明"></a>举例说明</h3><p>如图：</p>
<p><img src="https://i.loli.net/2021/04/14/R34iqleYaAKbyUg.png" alt="graph.png"></p>
<p>其中一个欧拉路径为4 5 8 7 6 8 9 1 5 3 2 4 6</p>
<p>假设我们走路径4 6 8 5，在5处有3种选择：(3,4,1)但是只能走3 4，因为根据算法第二步当前往顶点1时，边集加入边5,1 此时去除已走边，图为</p>
<p><img src="https://i.loli.net/2021/04/14/2IEPsTCQzXqSfB8.png" alt="graph1.png"></p>
<p>此时如果走5,1边，剩下的图将变成不连通图，即边(5,1)为割边。以此类推。</p>
<h3 id="算法具体步骤"><a href="#算法具体步骤" class="headerlink" title="算法具体步骤"></a>算法具体步骤</h3><p>基于DFS实现，首先运用DFS判断图是否是一个连通图，若图连通，则根据欧拉图定理2：除有2个顶点度为奇数外，其他顶点度都为偶数。判断图中奇数度的点是否为2个或0个，若为2个则有欧拉路径，如果0个则有欧拉回路，对于欧拉路径，我们选择二个点中之一作为DFS起点。运用栈作为物理存储结构。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//dfs</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">    st[top++] = x;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= N; ++i) &#123;</span><br><span class="line">        <span class="keyword">if</span> (graph[x][i]) &#123;</span><br><span class="line">            graph[x][i] = graph[i][x] = <span class="number">0</span>; <span class="comment">// 删除此边</span></span><br><span class="line">            dfs(i);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//fleury</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fleury</span><span class="params">(<span class="keyword">int</span> ss)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> bridge;</span><br><span class="line">    top=<span class="number">0</span>;</span><br><span class="line">    stk[top++] = ss; <span class="comment">// 将起点放入Euler路径中</span></span><br><span class="line">    <span class="keyword">while</span> (top &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        brige = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= N; i++) &#123; <span class="comment">// 试图搜索一条边不是割边（桥） </span></span><br><span class="line">            <span class="keyword">if</span> (graph[st[top<span class="number">-1</span>]][i]) &#123;</span><br><span class="line">                brige = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (brige) &#123; <span class="comment">// 如果没有点可以扩展，输出并出栈</span></span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, st[top--]);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; <span class="comment">// 否则继续搜索欧拉路径</span></span><br><span class="line">            dfs(st[top--]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="性能分析"><a href="#性能分析" class="headerlink" title="性能分析"></a>性能分析</h3><p>单独分析Fleury算法时间复杂度为O(|E(G)|)</p>
<p>Fleury算法需要基于DFS实现，用邻接矩阵存储图，DFS时间复杂度为$O(n^2)$，Fleury算法总的时间复杂度为$O(n^2)$。</p>
<h2 id="哈密尔顿问题"><a href="#哈密尔顿问题" class="headerlink" title="哈密尔顿问题"></a>哈密尔顿问题</h2><h3 id="问题概述-1"><a href="#问题概述-1" class="headerlink" title="问题概述"></a>问题概述</h3><p>欧拉路径问题是寻找一条路径能够遍历图中所有的边，而哈密尔顿问题是在寻找一条遍历图中所有点的基本路径。</p>
<p>在无向图G=&lt;V,E&gt;中，遍历G中每个顶点一次且仅一次的路径称为哈密尔顿路径，遍历G中每个顶点一次且仅一次的回路称为哈密尔顿回路。具有哈密尔顿回路的图称为哈密尔顿图。哈密尔顿问题是一类问题的总称。</p>
<h3 id="求解算法思想-1"><a href="#求解算法思想-1" class="headerlink" title="求解算法思想"></a>求解算法思想</h3><p>由于哈密尔顿问题被证明为一个NP问题，即非多项式问题，时间复杂度为$O(K^N)$。</p>
<p>首先需要判断一个图是否为哈密尔顿图，也就是是否存在哈密尔顿路径，但是证明找到一个图是否为哈密尔顿图的非平凡充要条件：</p>
<ol>
<li>若图的最小度不小于顶点数的一半，则图是哈密顿图；</li>
<li>若图中每一对不相邻的顶点的度数之和不小于顶点数，则图是哈密顿图。</li>
</ol>
<p><strong>若图为汉密尔顿图，可以运用一种算法思想：</strong></p>
<ol>
<li>从某个顶点v开始，遍历访问所有相邻顶点。</li>
<li>如果相邻顶点u被访问过，但图中的其他顶点还存在没有访问的顶点，则进行回溯，同时让已访问的顶点v未被访问。</li>
<li>重复1,2步骤，直到满足相邻顶点访问过且其为初始顶点，且图中所有顶点都被访问过。</li>
</ol>
<p>以上哈密尔顿回路的求解方法，并不需要遍历所有顶点组成的路径全排列，在两个顶点之间没有边的情况下，则不会形成一条路径，也就是剪枝。</p>
<h3 id="举例说明-1"><a href="#举例说明-1" class="headerlink" title="举例说明"></a>举例说明</h3><p>如图</p>
<p><img src="https://i.loli.net/2021/04/14/puSMDRKN2Ohj6b9.png" alt="graph2.png"></p>
<p>从顶点0开始访问图，开始遍历相邻顶点，假设路径为0-1.</p>
<p>继续dfs路径为0-1-2，现在下一步dfs要访问0，但是0已经被访问过了，现在已经满足哈密尔顿回路的第一个条件，从起点回到了起点。</p>
<p>但是图中还有剩余顶点没有被访问，此时需要<strong>回溯</strong>操作，同时将此时访问的顶点标记为没有访问过。这样做的原因是: 此时搜索的路径是一条死路, 需要回退并重新开始搜索。而回退的方式就是利用深度优先搜索的回溯特性, 重新搜索则需要将之前搜索过程中标记的访问的状态置为空。</p>
<p>回溯到1后dfs，由于相邻顶点3还未访问，访问3，形成路径0-1-3，此时与上方情况相同，形成死路，需要回溯，将1标记为未访问，返回到顶点0开始dfs，新路径为0-2，以此类推，可以得到路径0-2-1-3或0-3-1-2。</p>
<h3 id="算法具体步骤-1"><a href="#算法具体步骤-1" class="headerlink" title="算法具体步骤"></a>算法具体步骤</h3><ol>
<li>从某个顶点v开始，DFS访问所有相邻顶点。</li>
<li>如果相邻顶点u被访问过，但图中的其他顶点还存在没有访问的顶点，则进行回溯，同时标记顶点v未被访问。</li>
<li>重复1,2步骤，直到满足相邻顶点访问过且其为初始顶点，且图中所有顶点都被访问过。</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">isHamilton</span><span class="params">(<span class="keyword">int</span> D[],<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> isHp=<span class="number">1</span>,isHg=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> i=<span class="number">0</span>,j=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n;i++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;n;j++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(D[i]+D[j]&lt;(n<span class="number">-1</span>))&#123;</span><br><span class="line">                isHp=<span class="number">0</span>;isHg=<span class="number">0</span>;<span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(D[i]+D[j]&lt;n)&#123;</span><br><span class="line">                isHg=<span class="number">0</span>;<span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(isHp==<span class="number">0</span>)&#123;<span class="keyword">break</span>;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(isHg==<span class="number">1</span>)&#123;<span class="built_in">printf</span>(<span class="string">&quot;为哈密尔顿图\n&quot;</span>);&#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>((isHp==<span class="number">1</span>)&amp;&amp;(isHg==<span class="number">0</span>))&#123;<span class="built_in">printf</span>(<span class="string">&quot;存在哈密尔顿路\n&quot;</span>);&#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;<span class="built_in">cout</span>&lt;&lt;<span class="string">&quot;不存在哈密尔顿路&quot;</span>&lt;&lt;<span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="性能分析-1"><a href="#性能分析-1" class="headerlink" title="性能分析"></a>性能分析</h3><p>此算法为暴力搜索，时间复杂度为$O(n!<em>n)$，我们可以利用状态压缩动态规划，我们可以将时间复杂度降低到$O(n^2</em>n^3)$ ，具体算法是建立方程f(i,S,j)，表示经过了i个节点，节点都是集合S的，到达节点j时的最短路径。每次我们都按照点j所连的节点进行转移。</p>
<h2 id="中国邮递员问题-CPP"><a href="#中国邮递员问题-CPP" class="headerlink" title="中国邮递员问题(CPP)"></a>中国邮递员问题(CPP)</h2><h3 id="问题概述-2"><a href="#问题概述-2" class="headerlink" title="问题概述"></a>问题概述</h3><p>邮递员在某一地区的信件投递路程问题。邮递员每天从邮局出发，走遍该地区所有街道再返回邮局，问题是他应如何安排送信的路线可以使所走的总路程最短。用图论的语言描述，给定一个连通图G，每边e有非负权)，要求一条回路经过每条边至少一次，且满足总权最小。</p>
<h3 id="求解算法思想-2"><a href="#求解算法思想-2" class="headerlink" title="求解算法思想"></a>求解算法思想</h3><p>在一个具有非负权的赋权连通图G中，找出一条权最小的环游，这种环游称作最优环游。</p>
<p>如果是一个欧拉图，可以直接运用Fleury算法求出欧拉回路，可以得到解答。</p>
<p>当不是欧拉图时，有的边不得不通过大于等于2次，此时需要求解哪些边通过大于等于2次可以让权值最小。</p>
<p> 在1973年埃德蒙兹（J.Edmonds）和约翰逊（E.L.Johnson)给出了多项式次时间解法和证明，<strong>一般解法</strong>可以归纳为：</p>
<ol>
<li>将重复走过的边作为重边添加到图中。</li>
<li>添加重边成为欧拉图。</li>
<li>令添加的边权和最小。</li>
<li>Fleury算法找到欧拉回路。</li>
</ol>
<p>我们添加重边的目标为消除奇次顶点，这样才能使它变成一个欧拉图。</p>
<p>重边的添加方法：</p>
<ul>
<li>连接所有k个奇次顶点对的k条无公共边的最短路径。</li>
<li>边权和最小</li>
</ul>
<h3 id="举例说明-2"><a href="#举例说明-2" class="headerlink" title="举例说明"></a>举例说明</h3><p>如图，图中奇度顶点为V1,V6,V3,V9。找到奇度顶点间的最短路径V1-&gt;V6=4 ,V3-&gt;V9=2。</p>
<p>构造图以顶点为V1,V3,V6,V9，边权为最短路径边权和。找到构造图最小权完美匹配M。</p>
<p>沿M对应的最短路径添加重边。</p>
<p>运行Fleury算法。</p>
<h3 id="算法具体步骤-2"><a href="#算法具体步骤-2" class="headerlink" title="算法具体步骤"></a>算法具体步骤</h3><p>Edmonds-Johnson算法：</p>
<ol>
<li>找到所有2k个奇度顶点间的最短路径。</li>
<li>构造一个完全图$K_2k$:顶点为2k个奇度顶点，边权为最短路的边权和，运用Dijkstra算法。</li>
<li>找到完全图的最小权完美匹配M。</li>
<li>沿M对应的最短路径添加重边</li>
</ol>
<p>CPP问题解决方法</p>
<ol>
<li>判断图中奇度顶点个数，若为欧拉图，直接运行Fleury算法。</li>
<li>若不为欧拉图，运行Edmonds-Johnson算法构造新的欧拉图，运行Fleury算法。</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Floyd</span> <span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; graph, <span class="keyword">int</span> N)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k=<span class="number">1</span>; k&lt;=N; ++k) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=N; ++i) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">1</span>; j&lt;=N; ++j) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i == j) &#123;</span><br><span class="line">                        <span class="keyword">continue</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span> (graph[i][k]!=<span class="number">-1</span> &amp;&amp; graph[k][j]!=<span class="number">-1</span>) &#123;</span><br><span class="line">                        graph[i][j] = graph[i][j]==<span class="number">-1</span> ? graph[i][k]+graph[k][j] : min (graph[i][j], graph[i][k]+graph[k][j]);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">shortestPath</span> <span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; graph, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; dev, <span class="keyword">int</span> N)</span> </span>&#123;</span><br><span class="line">        Floyd (graph, N);</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; odds;</span><br><span class="line">        odds.push_back(<span class="number">0</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=N; ++i) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dev[i]&amp;<span class="number">1</span>) &#123;</span><br><span class="line">                odds.push_back(i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> ods = odds.size()<span class="number">-1</span>;</span><br><span class="line"> </span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">((<span class="number">1</span>&lt;&lt;ods), <span class="number">-1</span>)</span></span>;</span><br><span class="line">        dp[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;(<span class="number">1</span>&lt;&lt;ods); ++i) &#123;</span><br><span class="line">            <span class="keyword">int</span> x = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">while</span> ((<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>)) &amp; i) &#123;</span><br><span class="line">                ++x;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> y=x+<span class="number">1</span>; y&lt;=ods; ++y) &#123;</span><br><span class="line">                <span class="keyword">if</span> ((<span class="number">1</span>&lt;&lt;(y<span class="number">-1</span>)) &amp; i) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                dp[i|(<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>))|(<span class="number">1</span>&lt;&lt;(y<span class="number">-1</span>))] = dp[i] != <span class="number">-1</span> &amp;&amp; graph[odds[x]][odds[y]] != <span class="number">-1</span> ? dp[i|(<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>))|(<span class="number">1</span>&lt;&lt;(y<span class="number">-1</span>))] == <span class="number">-1</span> ? dp[i]+graph[odds[x]][odds[y]] : min(dp[i|(<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>))|(<span class="number">1</span>&lt;&lt;(y<span class="number">-1</span>))], dp[i]+graph[odds[x]][odds[y]]) : dp[i|(<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>))|(<span class="number">1</span>&lt;&lt;(y<span class="number">-1</span>))];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;(<span class="number">1</span>&lt;&lt;ods); ++i) &#123;</span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; dp[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; dp[(<span class="number">1</span>&lt;&lt;ods)<span class="number">-1</span>] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        <span class="keyword">return</span> dp[(<span class="number">1</span>&lt;&lt;ods)<span class="number">-1</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="卖货郎问题-TSP"><a href="#卖货郎问题-TSP" class="headerlink" title="卖货郎问题(TSP)"></a>卖货郎问题(TSP)</h2><h3 id="问题概述-3"><a href="#问题概述-3" class="headerlink" title="问题概述"></a>问题概述</h3><p>给定一系列城市和每对城市之间的距离，求解访问每一座城市一次并回到起始城市的最短回路。</p>
<p>TSP是一个NP难问题。</p>
<p>图论描述：求赋权连通图中经过每个顶点恰一次且全权和最小的边</p>
<h3 id="求解算法思想-3"><a href="#求解算法思想-3" class="headerlink" title="求解算法思想"></a>求解算法思想</h3><p>最容易想到的是递归遍历，但是最差时间复杂度为$O(n!)$，所以我们运用贪心算法的思想，寻找局部最优解，我们可以从一个城市即顶点开始每次选择一个城市，直到所有的城市被走完，每次选择下一个城市我们只考虑当前所走过的路径最短。我们还可以运用近似算法，高效的找出最优解。近似算法有<strong>临近点法，最小生成树法，最小权匹配法以及Kernighan-Lin方法。</strong></p>
<p><strong>临近点法</strong>的基本思路是总是贪心地选择最近的未访问邻接点前行。</p>
<h3 id="举例说明-3"><a href="#举例说明-3" class="headerlink" title="举例说明"></a>举例说明</h3><p><img src="https://i.loli.net/2021/04/14/9cpzlF5x7IHE8VD.png" alt="TSP1.png"></p>
<p>假设从A点出发，我们寻找邻接点中权值最小的边即a-b或a-e。</p>
<p>假如选择a-b，在b点继续选择即b-d，以此类推，最后可以得到总路径权值和=5+7+8+16+12=48.</p>
<h3 id="算法具体步骤-3"><a href="#算法具体步骤-3" class="headerlink" title="算法具体步骤"></a>算法具体步骤</h3><p>针对TSP问题，使用临近点法的求解的过程为：<br>    1.从某一个城市开始，每次选择一个城市，直到所有的城市被走完。<br>    2.每次在选择下一个城市的时候，只考虑当前情况，保证迄今为止经过的路径总距离最小。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这个城市k是否出现过了</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isShowed</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; cityNum ; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (s[i] == k) &#123;</span><br><span class="line">            <span class="keyword">return</span> YES ;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> NO;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">clostCityDistance</span><span class="params">(<span class="keyword">int</span> currentCity)</span> </span>&#123;</span><br><span class="line"><span class="comment">// 距离当前城市最近的下一个城市</span></span><br><span class="line"> <span class="keyword">int</span> tempClosest = <span class="number">9999</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; cityNum; i++) &#123;</span><br><span class="line"> 	<span class="keyword">if</span> ( isShowed(i) == NO &amp;&amp; distance[i][s[currentCity]] &lt;tempClosest ) &#123;</span><br><span class="line"> 		tempClosest = distance[i][s[currentCity]] ;</span><br><span class="line"> 		s[currentCity+<span class="number">1</span>] = i ; </span><br><span class="line"> 	&#125;</span><br><span class="line"><span class="comment">// 判断是否应该结束了,  如果s[]中有一个还是初始值-1的话,说明不该结束,继续寻找下一个城市.</span></span><br><span class="line"> <span class="keyword">bool</span> shouldFinish = YES;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; cityNum; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> ( s[i] == <span class="number">-1</span> ) &#123;</span><br><span class="line">            shouldFinish = NO ;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (shouldFinish == NO) &#123;</span><br><span class="line">        clostCityDistance(s[currentCity+<span class="number">1</span>]);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 剩下就是初始条件了, 初始化出发的城市,可以是0,1,2,3任意一个</span></span><br><span class="line">      s[<span class="number">0</span>] = <span class="number">0</span>; <span class="comment">// s[0] = 1 ;</span></span><br><span class="line">    clostCityDistance(s[<span class="number">0</span>]);</span><br></pre></td></tr></table></figure>

<h3 id="性能分析-2"><a href="#性能分析-2" class="headerlink" title="性能分析"></a>性能分析</h3><p>邻接点法本质是贪心算法，此算法性能与初始点有关，最差情况下要到第n个点才能求得最短路径，时间复杂度为$O(v^2)$</p>

    </div>
    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/C/" rel="tag"><i class="fa fa-tag"></i> C++</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/14/C++11RANDOM/" rel="prev" title="C++随机数大总结">
      <i class="fa fa-chevron-left"></i> C++随机数大总结
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/14/csapp-lab0-1/" rel="next" title="预备--一些简单汇编代码与debug操作">
      预备--一些简单汇编代码与debug操作 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E9%81%8D%E5%8E%86"><span class="nav-number">1.</span> <span class="nav-text">图的遍历</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AC%A7%E6%8B%89%E8%B7%AF%E5%BE%84"><span class="nav-number">1.1.</span> <span class="nav-text">欧拉路径</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E6%A6%82%E8%BF%B0"><span class="nav-number">1.1.1.</span> <span class="nav-text">问题概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B1%82%E8%A7%A3%E7%AE%97%E6%B3%95%E6%80%9D%E6%83%B3"><span class="nav-number">1.1.2.</span> <span class="nav-text">求解算法思想</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E"><span class="nav-number">1.1.3.</span> <span class="nav-text">举例说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E5%85%B7%E4%BD%93%E6%AD%A5%E9%AA%A4"><span class="nav-number">1.1.4.</span> <span class="nav-text">算法具体步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90"><span class="nav-number">1.1.5.</span> <span class="nav-text">性能分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%93%88%E5%AF%86%E5%B0%94%E9%A1%BF%E9%97%AE%E9%A2%98"><span class="nav-number">1.2.</span> <span class="nav-text">哈密尔顿问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E6%A6%82%E8%BF%B0-1"><span class="nav-number">1.2.1.</span> <span class="nav-text">问题概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B1%82%E8%A7%A3%E7%AE%97%E6%B3%95%E6%80%9D%E6%83%B3-1"><span class="nav-number">1.2.2.</span> <span class="nav-text">求解算法思想</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E-1"><span class="nav-number">1.2.3.</span> <span class="nav-text">举例说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E5%85%B7%E4%BD%93%E6%AD%A5%E9%AA%A4-1"><span class="nav-number">1.2.4.</span> <span class="nav-text">算法具体步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90-1"><span class="nav-number">1.2.5.</span> <span class="nav-text">性能分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%AD%E5%9B%BD%E9%82%AE%E9%80%92%E5%91%98%E9%97%AE%E9%A2%98-CPP"><span class="nav-number">1.3.</span> <span class="nav-text">中国邮递员问题(CPP)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E6%A6%82%E8%BF%B0-2"><span class="nav-number">1.3.1.</span> <span class="nav-text">问题概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B1%82%E8%A7%A3%E7%AE%97%E6%B3%95%E6%80%9D%E6%83%B3-2"><span class="nav-number">1.3.2.</span> <span class="nav-text">求解算法思想</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E-2"><span class="nav-number">1.3.3.</span> <span class="nav-text">举例说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E5%85%B7%E4%BD%93%E6%AD%A5%E9%AA%A4-2"><span class="nav-number">1.3.4.</span> <span class="nav-text">算法具体步骤</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%96%E8%B4%A7%E9%83%8E%E9%97%AE%E9%A2%98-TSP"><span class="nav-number">1.4.</span> <span class="nav-text">卖货郎问题(TSP)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E6%A6%82%E8%BF%B0-3"><span class="nav-number">1.4.1.</span> <span class="nav-text">问题概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B1%82%E8%A7%A3%E7%AE%97%E6%B3%95%E6%80%9D%E6%83%B3-3"><span class="nav-number">1.4.2.</span> <span class="nav-text">求解算法思想</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E-3"><span class="nav-number">1.4.3.</span> <span class="nav-text">举例说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%97%E6%B3%95%E5%85%B7%E4%BD%93%E6%AD%A5%E9%AA%A4-3"><span class="nav-number">1.4.4.</span> <span class="nav-text">算法具体步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90-2"><span class="nav-number">1.4.5.</span> <span class="nav-text">性能分析</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Sekla"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Sekla</p>
  <div class="site-description" itemprop="description">Sekla</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ShenTiao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShenTiao" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:584892986@qq.com" title="E-Mail → mailto:584892986@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1416875904&auto=0&height=66"></iframe>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sekla</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">288k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:22</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":500},"mobile":{"show":false},"react":{"opacity":0.9},"log":false});</script></body>
</html>

<script type="text/javascript" src="/js/src/love.js"></script>
